package com.xiaolin.spring.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @ClassName InstanceFactoryInjectTest
 * @Description TODO
 * @Detail 实例工厂注入,一个工厂可创建多个不同的bean实例
 * @Author MyPC
 * @Date 2020/10/27
 * @Version 1.0
 */
public class InstanceFactoryInjectTest {
    private static DemoBean demoBean=new DemoBean();

    private static DemoBean2 demoBean2;

    public DemoBean getInstance(){
        return demoBean;
    }
    public DemoBean2 getInstance2(int age,String name){
        if(demoBean2 == null){
            demoBean2=new DemoBean2(age,name);
        }
        return demoBean2;
    }

    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("instance-factory.xml");
        DemoBean demoBean = applicationContext.getBean("demoBean", DemoBean.class);
        DemoBean2 demobean2 = applicationContext.getBean("demoBean2", DemoBean2.class);
        //获取实例bean的运行时类型
        Class<?> type = applicationContext.getType("demoBean2");
        System.out.println(demoBean);
        System.out.println(demobean2);
        System.out.println(type);
    }
}
